obsluga_www() {
webServer.handleClient(); //----- serwer HTTP -----//
if (WiFi.status() == WL_CONNECTED) {
if ( millis() > CzasDoURL ) {
CzasDoURL = millis() + Co_ILE_NA_URL + random( 0, 500 );
//----- WWW (RPi)
byte static cnt;
if ( ++cnt >= Co_ILE_NA_URL_LOCAL_HOST ) cnt = 0;
for (byte www = 0; www < LICZBA_HOSTOW; www++) {
WiFiClient client;
const int httpPort = 80;
const char* host;
if ( www == 0 ) host = DATA_HOST_REMOTE_1;
else if ( www == 1 ) host = DATA_HOST_REMOTE_2;
else if ( www == 2 ) host = DATA_HOST_LOCAL_2;
else host = DATA_HOST_LOCAL_1;
if ( host == "" ) { // Jesli nazwa hosta istnieje
printDebug( ANSI_PEN_YELLOW"\n\rPomijam wylaczony host"ANSI_PEN_WHITE );
}
else {
if (!client.connect(host, httpPort)) {
printDebug( ANSI_PEN_RED"\n\rERROR: connection failed"ANSI_PEN_WHITE );
}
else {
String url = LOGGER_URL;
char txt[1000];
if ( ! ornoOnLine ) { // Jesli off-line
url += "moc='off-line"; // Gdy nie ma komunikacji z licznikiem
url += "&pf='off-line";
}
else {
url += "moc=";
sprintf( txt, "%d,%03d", MocCzynna / 1000, MocCzynna % 1000 );
url += txt;
url += "&pf=";
sprintf( txt, "%d,%03d", WspolczynnikMocy / 1000, WspolczynnikMocy % 1000 );
url += txt;
url += "&var=";
sprintf( txt, "%d,%03d", MocBierna / 1000, MocBierna % 1000 );
url += txt;
url += "&VA=";
sprintf( txt, "%d,%03d", MocPozorna / 1000, MocPozorna % 1000 );
url += txt;
url += "&I=";
sprintf( txt, "%d,%03d", Prad / 1000, Prad % 1000 );
url += txt;
url += "&U=";
sprintf( txt, "%d,%02d", Napiecie / 100, Napiecie % 100 );
url += txt;
url += "&f=";
sprintf( txt, "%d,%02d", Czestotliwosc / 100, Czestotliwosc % 100 );
url += txt;
url += "&EP=";
sprintf( txt, "%d,%03d", EnergiaCzynna / 1000, EnergiaCzynna % 1000 );
url += txt;
url += "&Evar=";
sprintf( txt, "%d,%03d", EnergiaBierna / 1000, EnergiaBierna % 1000 );
url += txt;
if ( fl_bzdura ) { // Nienormalne ramki
url += "&modbus=";
for ( byte x = 0; x < fl_bzdura; x++) {
sprintf( txt, "%02x_", BufOdpOrno[x] );
url += txt;
}
url += "&st=";
if ( rdORNOfull < LICZBA_PYTAN_ORNO ) strcpy( txt, "no-complit" ); // jeśli dane niekompletne
else sprintf( txt, "%d", stModbus );
url += txt;
if ( www == LICZBA_HOSTOW - 1 ) fl_bzdura = false; // Flage kasujemy gdy wysylka na ostatni host
}
}
if ( ip[3] == 52 ) url += "&id=glowny"; //sprintf( txt, "%d", ip[3] ); url += "&id=";
else if ( ip[3] == 53 ) url += "&id=warsztat";
else if ( ip[3] == 54 ) url += "&id=przenosny";
else {
url += "&id=nowy";
sprintf( txt, " %d", ip[3] ); url += txt;
}
uint32_t tim = timWwwStat = micros();
timWwwEnd = 0;
if ( www == 0 || www == 1 || ( www >= 2 && !cnt) ) { // na zdalny (www==0 lub 1) zawsze, lokalny (www<>0 i 1) tylko gdy cnt==0
#ifdef DEBUG_URL
if ( www >= 2 ) printDebug( "\n\rSend >>> Local <<< host '" );
else printDebug( "\n\rSend Remote host '" );
printDebug( (char*)host );
strcpy( txt, url.c_str() ); printDebug( txt );
sprintf( txt, " [cnt=%d] ", cnt ); printDebug(txt);
printDebug( "'" );
#endif
client.print( String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n" );
#ifdef DEBUG_URL
sprintf( txt, "\n\r Send GET %d.%02dms ", (micros() - tim) / 1000, (micros() - tim) % 1000 ); printDebug( txt ); //Czas na jaki angarzowany jest uC
#endif
#ifdef REPLY_WWW
//----- Odpowiedz z WWW
uint32_t timeout = millis() + OVERTIME_REPLY_WWW;
while ( client.connected() || client.available() ) {
if ( millis() >= timeout ) break;
if ( client.available() ) {
if ( ! timWwwEnd ) timWwwEnd = micros();
String line = client.readStringUntil('\n');
#if defined( DEBUG_PRINT_REP_WWW ) && defined( DEBUG_UART1 )
printDebug( ANSI_PEN_CYAN"\n\rOdpowiedz ze strony \n\r"ANSI_PEN_YELLOW );
Serial1.println(line); //todo: printDebug
printDebug( ANSI_PEN_WHITE"\n\r" );
#endif
}
}
#ifdef DEBUG_URL
sprintf( txt, "\n\r Read HTML %d.%02dms", (timWwwEnd - timWwwStat) / 1000, (timWwwEnd - timWwwStat) % 1000 ); printDebug( txt ); // Czas odpowiedzi z WWW
#endif
#endif
}
}
}
}
}
} // END if ( WiFi.status() == WL_CONNECTED )
}